package swing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;

import carlyang.Out;

public class FlowLayoutTest {
	public static Path testpath = Paths.get("E:/adt_workplace/1.txt");
	public static Path path = Paths.get("E:/adt_workplace/2.txt");
	public static Path path2 = Paths
			.get("E:/adt_workplace/test/src/carlyang/FileRead.java");
	public static Path debugPath = Paths.get("E:/adt_workplace/debug.txt");
	public static Path debugPath2 = Paths
			.get("F:/Android/adt_workspace/debug.txt");

	public static Charset charsetChineseSimple = Charset.forName("GB2312");
	public static Charset charsetDef = Charset.defaultCharset();// GBK
	public static Charset charsetUTF_8 = Charset.forName("UTF-8");
	public static Charset charsetUsascii = Charset.forName("US-ASCII");

	public FlowLayoutTest() {
		// TODO Auto-generated constructor stub
	}

	private static void constructGUI() {
		// JFrame.setDefaultLookAndFeelDecorated(true);

		JFrame frame = new JFrame("FlowLayout Test");
		// frame.setLayout(new FlowLayout());
		frame.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 在Swing中可以不需这个设置要也能关闭

		// frame.add(new Button("BT1"));
		// frame.add(new Button("BT2"));
		// frame.add(new Button("BT3"));
		// frame.add(new Button("BT4"));
		// read text file
		String text = "";
		Out.println("Default Charset:" + Charset.defaultCharset());
		Out.println("Dir:" + debugPath2.getParent());
		try (BufferedReader bReader = Files.newBufferedReader(debugPath2,
				charsetChineseSimple)) {
			String textTmp = bReader.readLine();
			while (textTmp != null) {
				text += textTmp + "\n";
				// System.out.println(textTmp);
				textTmp = bReader.readLine();
			}
		} catch (Exception e) {
			// TODO: handle exception
			// System.out.println(e.toString());
			e.printStackTrace();
		}
		JTextArea textArea = new JTextArea(text, 20, 20);
		// textArea.setPreferredSize(null);
		textArea.setPreferredSize(new Dimension(100, 100));
		textArea.setLineWrap(true);

		JTextArea textArea2 = new JTextArea(text, 20, 20);
		textArea2.setPreferredSize(new Dimension(100, 100));
		textArea2.setLineWrap(true);
		JScrollPane jScrollPane = new JScrollPane(textArea2,
				ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
				ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);

		frame.add(textArea);
		frame.add(jScrollPane);
		frame.pack();
		frame.setVisible(true);
		Out.println("myFrame.getWidth=" + frame.getWidth());
		Out.println("constructGUI thread name:"
				+ Thread.currentThread().getName());
	}

	// 使用BoxLayout布局
	private static void constructGUIBoxLayout() {
		// JFrame.setDefaultLookAndFeelDecorated(true);

		JFrame frame = new JFrame("BoxLayout Test");
		// frame.setLayout(new FlowLayout());
		// frame.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));// 单列往下流
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 在Swing中可以不需这个设置要也能关闭
		frame.setBackground(Color.green);// 无效果
		// frame.setBounds(500, 300, 200, 200);
		frame.add(new JButton("BT1"));
		frame.add(new JButton("BT2"));
		frame.add(new JButton("BT3"));
		frame.add(new JButton("BT4"));
		frame.add(new JButton("BT5"));

		frame.pack();
		frame.setVisible(true);
		Out.println("myFrame.getWidth=" + frame.getWidth());
		Out.println("constructGUI thread name:"
				+ Thread.currentThread().getName());
	}

	// 使用GridLayout布局
	private static void constructGUIGridLayout() {
		// JFrame.setDefaultLookAndFeelDecorated(true);

		JFrame frame = new JFrame("GridLayout Test");
		// frame.setLayout(new FlowLayout());
		// frame.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		// frame.setLayout(new
		// BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));//单列往下流
		frame.setLayout(new GridLayout(5, 5, 5, 5));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 在Swing中可以不需这个设置要也能关闭
		frame.setBackground(Color.green);// 无效果
		// frame.setBounds(500, 300, 100, 100);

		for (int i = 1; i <= 100; i++) {
			JLabel jLabel$i = new JLabel("Jlable" + i);
			frame.add(jLabel$i);
		}
		// Out.println("myFrame size=" + frame.getComponents().length);
		frame.pack();
		frame.setVisible(true);
		Out.println("myFrame.getWidth=" + frame.getWidth());
		Out.println("constructGUI thread name:"
				+ Thread.currentThread().getName());
	}

	// 不使用LayoutManager布局
	private static void constructGUINoLayout() {
		// JFrame.setDefaultLookAndFeelDecorated(true);

		JFrame frame = new JFrame("NoLayoutManager Test");
		frame.setLayout(null);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 在Swing中可以不需这个设置要也能关闭
		frame.setBackground(Color.green);// 无效果
		frame.setBounds(500, 300, 500, 300);// 位置500,300;大小500x300
		JButton button1 = new JButton("BT1");
		button1.setBounds(0, 0, 50, 50);// 相对于frame的坐标位置
		JButton button2 = new JButton("BT2");
		button2.setBounds(100, 20, 50, 50);

		frame.add(button1);
		frame.add(button2);
		// frame.setSize(200, 200);//默认位置为0,0,若之前已设置过，则不修改位置坐标，只修该Frame的大小
		frame.setVisible(true);
		JButton button3 = new JButton("BT3");
		button3.setBounds(160, 20, 50, 50);
		frame.add(button3);
		Out.println("myFrame.getWidth=" + frame.getWidth());
		Out.println("constructGUI thread name:"
				+ Thread.currentThread().getName());
	}

	private static JFrame jFrame = null;
	private static int lableNuber = 1;

	private static void EventTest() {
		JFrame.setDefaultLookAndFeelDecorated(true);
		JFrame frame = new JFrame("Event Test");
		jFrame = frame;
		// frame.setLayout(new FlowLayout());
		// frame.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
		// frame.setLayout(new
		// BoxLayout(frame.getContentPane(),BoxLayout.Y_AXIS));//单列往下流
		frame.setLayout(new GridLayout(10, 10, 5, 5));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 在Swing中可以不需这个设置要也能关闭
		JButton addButton = new JButton("add compnent");
		addButton.setSize(20, 30);
		frame.add(addButton);
		int i = 1;
		for (; i <= 9; i++) {
			JLabel jLabel$i = new JLabel("JLable" + i);
			frame.add(jLabel$i);
		}
		lableNuber = i;
		addButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				JButton sourceButton = (JButton) e.getSource();
				Out.println("JButton text:" + sourceButton.getText());
				Out.println("Command:" +e.getActionCommand()+",ID"+e.getID());
				jFrame.add(new JLabel("JLable" + lableNuber++));
				jFrame.pack();
				JOptionPane.showMessageDialog(null, "add a JLable"+(lableNuber-1)+"  "+sourceButton.getText());
				// jFrame.setVisible(true);
			}
		});

		frame.pack();
		frame.setLocation(600, 300);
		frame.setVisible(true);

		Out.println("myFrame.getWidth=" + frame.getWidth());
		Out.println("constructGUI thread name:"
				+ Thread.currentThread().getName());
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				// constructGUI();// 使用static方法或创建对象
				// constructGUIBoxLayout();
				// constructGUIGridLayout();
				// constructGUINoLayout();
				EventTest();
			}
		});
	}

}
